package com.demo.house.controller;

import com.demo.house.common.Result;
import com.demo.house.entity.HouseSourse;
import com.demo.house.service.HouseSourseService;
import com.demo.house.vo.RequestVo.*;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;

@RequestMapping(value = "/HouseSourse")
@RestController
public class HouseSourseController {

    @Autowired
    private HouseSourseService houseSourseService;

    /**房东根据id查询
     * @param id
     * @return
     */
    @GetMapping("selectUserId/{id}")
    public Result selectUserId(@PathVariable Integer id){
        return Result.getSuccess().setData(houseSourseService.selectUserId(id));
    }

    /*修改房源*/
    @PutMapping(value = "/update")
    public Result update(@RequestBody HouseSourse sourse){
        return Result.getSuccess().setData(houseSourseService.update(sourse));
    }
    /*删除房源*/
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.getSuccess().setData(houseSourseService.delete(id));
    }

    /*修改state字段，上下架*/
    @PutMapping("/update1")
    public Result updateStatus(@RequestBody HouseSourse houseSourse){
/*        HouseSourse houseSourse = new HouseSourse();
        houseSourse.setId(vo.getId());
        houseSourse.setState(vo.getState());*/
        return Result.getSuccess().setData(houseSourseService.updateState(houseSourse));
    }

    /*查询表所有数据*/
    @GetMapping("selectAll")
    public Result selectAll(){
        return Result.getSuccess().setData(houseSourseService.selectAll());
    }

    /*新增房源数据*/
    @PostMapping("insert")
    public Result insert(@RequestBody HouseSourse houseSourse){
        return Result.getSuccess().setData(houseSourseService.insertSelective(houseSourse));
    }
    /**详情，根据id查询房源
     * @return
     */
    @GetMapping("selectCaa/{id}")
    public Result selectCaa(@PathVariable Integer id){
        List<HouseUrlSourseVo> houseUrlSourseVos = houseSourseService.selectCaa(id);
       /* if (houseUrlSourseVos==null){
            return Result.getSuccess().setData("抱歉：您浏览的房子已下架或已被出租");
        }*/
        return Result.getSuccess().setData(houseUrlSourseVos);
    }
/*显示图片*/
    @SneakyThrows
    @GetMapping("getImg")
    public void getImg(Integer id,HttpServletResponse response){

        System.out.println(id);
        BufferedImage img = houseSourseService.getImg(id);
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(img,"jpg",outputStream);
        IOUtils.closeQuietly(outputStream);
        try {
            outputStream.flush();
        }finally {
            outputStream.close();
        }
    }

    /**
     * 模糊搜索
     * @param
     * @return
     */
    @GetMapping("/Blurry")
    public Result blurry(RequsetSourseVo reponseSourseVo){
        System.out.println(reponseSourseVo.toString());
        return Result.getSuccess().setData(houseSourseService.blurry(reponseSourseVo));
    }

    /**随机查询多个房源
     * @param
     * @return
     */
    @GetMapping("/Randomquery")
    public Result Randomquery(RandomqueryVo randomqueryVo){
        return Result.getSuccess().setData(houseSourseService.Randomquery(randomqueryVo));
    }

    /**浏览记录
     * @param
     * @return
     */
    @GetMapping("/BrowsingRecords")
    public Result getByids(int[] ids){
        System.out.println("拉莫斯电话"+ Arrays.toString(ids));
        return Result.getSuccess().setData(houseSourseService.getByids(ids));
    }

}
